<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="数据类型名称" prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          placeholder="请输入"
          clearable
        />
      </el-form-item>
      <el-form-item label="数据类型标识" prop="typeCode">
        <el-input
          v-model="queryParams.typeCode"
          placeholder="请输入"
          clearable
        />
      </el-form-item>
      <el-form-item label="运行状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="运行状态"
          clearable
          style="width: 240px"
        >
          <el-option label="运行" value="1" />
          <el-option label="停止" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column
        prop="typeName"
        label="数据类型名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="typeCode"
        label="数据类型标识"
        align="center"
      ></el-table-column>
      <el-table-column
        label="数据长度"
        align="center"
        prop="dataLength"
      ></el-table-column>
      <el-table-column
        label="数据表头标识"
        align="center"
        prop="startCode"
      ></el-table-column>
      <el-table-column
        label="数据结束标识"
        align="center"
        prop="endCode"
      ></el-table-column>
      <el-table-column
        label="地址"
        align="center"
        prop="dataInterface.interfaceMulticastAddress"
      ></el-table-column>
      <el-table-column
        label="端口号"
        align="center"
        prop="dataInterface.interfacePort"
      ></el-table-column>
      <el-table-column label="运行状态" align="center" key="status">
        <template slot-scope="scope">
          <el-button type="text" v-if="scope.row.status" @click="handleStatusChange(scope.row)">运行</el-button>
          <el-button type="text" v-else @click="handleStatusChange(scope.row)">停止</el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改部门对话框 -->
    <Edit
      v-if="dialogVisible"
      :dialogTitle="dialogTitle"
      :dialogOpen="dialogOpen"
      :dialogData="dialogData"
      @closeDialog="closeDialog"
    />
  </div>
</template>

<script>
import {
  listDataTypes,
  changeTypeStatus,
  removeDataType,
} from "@/api/statistical";
import Edit from "./components/edit.vue";

export default {
  name: "Datasource",
  components: { Edit },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      dataList: [],
      // 重新渲染表格状态
      refreshTable: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeName: undefined,
        typeCode: undefined,
      },
      sourceCodeDatas: [],
      //弹窗参数
      dialogVisible: false,
      dialogTitle: "",
      dialogOpen: false,
      dialogData: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listDataTypes(this.queryParams).then((response) => {
        console.log(224, response.rows);
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态修改
    handleStatusChange(row) {
      console.log(227, row);
      let text = row.status == 0 ? "运行" : "停止";
      this.$modal
        .confirm("确认要" + text + "该项吗？")
        .then(function () {
          return changeTypeStatus(row.id);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
          this.getList()
        })
        .catch(function () {
          // row.status = row.status == 0 ? 1 : 0;
        });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //关闭库相关
    closeDialog() {
      this.dialogVisible = false;
      this.dialogOpen = false;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      delete this.queryParams.status
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.dialogOpen = true;
      this.dialogTitle = "新建数据类型";
      this.dialogVisible = true;
      this.dialogData = {};
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(218, row);
      this.dialogOpen = true;
      this.dialogTitle = "编辑数据类型";
      this.dialogVisible = true;
      this.dialogData = row;
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("确定要删除该数据项？")
        .then(function () {
          return removeDataType([ids]);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
